﻿using System.Net;
using System.Net.Mail;
using System.Text;

namespace CoreLibrary
{
    public class MailUtil
    {
        /// <summary>
        /// 发件箱用户名
        /// </summary>
        private string UserName;

        /// <summary>
        /// 发件箱密码
        /// </summary>
        private string Password;

        /// <summary>
        /// 发件箱服务器地址
        /// </summary>
        private string Host;

        /// <summary>
        /// 发件箱端口号
        /// </summary>
        private int Port;

        private MailUtil()
        { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="host">服务器地址</param>
        /// <param name="port">端口号</param>
        public MailUtil(string userName, string password, string host, int port)
        {
            this.UserName = userName;
            this.Password = password;
            this.Host = host;
            this.Port = port;
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="to">收件人</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        public void SendEmail(string to, string subject, string body)
        {
            //如果未配置,直接跳过
            if (string.IsNullOrEmpty(to) || string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password) || string.IsNullOrEmpty(Host) || Port == 0)
                return;
            SmtpClient client = new SmtpClient(Host, Port)
            {
                EnableSsl = false,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(UserName, Password),
                DeliveryMethod = SmtpDeliveryMethod.Network
            };
            var msg = new MailMessage(UserName, to, subject, body) { BodyEncoding = Encoding.UTF8, IsBodyHtml = true };
            client.Send(msg);
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="to">收件人</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="type">类型（预约提交，订单提交，用户注册）</param>
        /// <param name="refId">关联Id</param>
        public void SendEmail(string to, string subject, string body, int type, int refId)
        {
            //如果未配置,直接跳过
            if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password) || string.IsNullOrEmpty(Host) || Port == 0)
                return;
            SmtpClient client = new SmtpClient(Host, Port)
            {
                EnableSsl = false,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(UserName, Password),
                DeliveryMethod = SmtpDeliveryMethod.Network
            };
            var msg = new MailMessage(UserName, to, subject, body) { BodyEncoding = Encoding.UTF8, IsBodyHtml = true };
            client.Send(msg);
        }
    }
}
